// 封装request方法
/**
 *
 */
const BASE_URL = 'https://api-hmugo-web.itheima.net'
const request = async ({ url, method, header = {}, data }) => {
  // loading
  uni.showLoading({
    title: '请求中...'
  })
  // 从本地获取token
  const token = uni.getStorageSync('ugo-token-150')
  if (token) {
    header.Authorization = token
  }
  const [error, res] = await uni.request({
    // 拼接后台请求完整地址
    url: BASE_URL + url,
    method,
    header,
    data
  })
  // 关闭loading
  uni.hideLoading()
  if (!error) {
    // 返回成功
    return {
      msg: res.data.meta,
      data: res.data.message
    }
  } else {
    return Promise.reject(error)
  }
}
export default request
